package com.wu.ssm.spring.mvc;


import com.wu.ssm.spring.ioc.BeanHelper;
import com.wu.ssm.spring.utils.ReflectionUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 处理器
 * Created by wzf on 2018/11/29/029.
 */
public class RequestMappingHandler  {

    /**
     * 请求controller
     */
    private Class<?> controllerClazz;

    /**
     * 处理方法
     */
    private Method method;


    public RequestMappingHandler(Class<?> controllerClazz, Method method) {
        this.controllerClazz = controllerClazz;
        this.method = method;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Class<?> getControllerClazz() {
        return controllerClazz;
    }

    public void setControllerClazz(Class<?> controllerClazz) {
        this.controllerClazz = controllerClazz;
    }



    public void doRequestMappingHandler(HttpServletRequest request, HttpServletResponse response){
        Object controllerBean = BeanHelper.getBean(controllerClazz);
        // todo 问题解决了，在invokeMethod方法中加个判断
        // 如果类成员有成员变量注入了 则调用第二个方法 否则第一个，如果注入失败 则调用出错？
        //ReflectionUtil.invokeMethod(controllerBean, method);
        ReflectionUtil.invokeMethod(controllerBean, method,new Object[] { request, response, null });
    }
}
